package com.vm.adapter;

import java.util.List;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.vm.activity.GoodsDetailsActivity;
import com.vm.commons.VMConst;
import com.vm.model.GoodsCollection;
import com.vm.utils.MyDisplayImageOptions;
import com.wwtscapp.apk.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("InflateParams")
public class GoodsCollectionAdapter extends BaseAdapter {
	private Context mContext;
	private List<GoodsCollection> listGoodsCollections;

	public GoodsCollectionAdapter(Context context, List<GoodsCollection> lists) {
		this.mContext = context;
		this.listGoodsCollections = lists;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listGoodsCollections.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.goods_collection_item, null);
			viewHolder = new ViewHolder();
			viewHolder.iv_goodsImage = (ImageView) convertView
					.findViewById(R.id.iv_goodsImage);
			viewHolder.tv_goodsName = (TextView) convertView
					.findViewById(R.id.tv_goodsName);
			viewHolder.tv_goodsPrice = (TextView) convertView
					.findViewById(R.id.tv_goodsPrice);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		GoodsCollection goodsCollection = listGoodsCollections.get(position);
		viewHolder.tv_goodsName.setTag(goodsCollection.getGoodsId());
		viewHolder.iv_goodsImage.setTag(goodsCollection.getImageUrl());
		if (goodsCollection.getName() != null) {
			viewHolder.tv_goodsName.setText(goodsCollection.getName());
		}
		if (goodsCollection.getPrice() != null) {
			viewHolder.tv_goodsPrice.setText(Float.toString(goodsCollection
					.getPrice()));
		}
		ImageLoader.getInstance().displayImage(
				VMConst.URL + goodsCollection.getImageUrl(),
				viewHolder.iv_goodsImage, MyDisplayImageOptions.getInstance());
		convertView.setOnClickListener(lv_listener);
		return convertView;
	}

	OnClickListener lv_listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(v.getContext(),
					GoodsDetailsActivity.class);
			ImageView iv_goodsImage = (ImageView) v
					.findViewById(R.id.iv_goodsImage);
			TextView tv_goodsName = (TextView) v
					.findViewById(R.id.tv_goodsName);
			String imageUrl = iv_goodsImage.getTag().toString();
			String goodsId = tv_goodsName.getTag().toString();
			intent.putExtra("imageUrl", imageUrl);
			intent.putExtra("goodsId", goodsId);
			v.getContext().startActivity(intent);
		}
	};

	static class ViewHolder {
		ImageView iv_goodsImage;
		TextView tv_goodsName;
		TextView tv_goodsPrice;
	}
}
